package com.sardine.base.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * 北京京东世纪贸易有限公司<br>
 * Created with IntelliJ IDEA.
 * Date:2015/12/3
 * Time:15:15
 *
 * @author daifeng<br>
 *         daifeng@jd.com
 * @since JDK 1.6 <br>
 * Copyright (c) 2015 JD System Incorporated All Rights Reserved.
 */
public class SystemProperties {

    private final static Logger logger = LoggerFactory.getLogger(SystemProperties.class);

    public static void main(String[] args){
//        Properties properties = System.getProperties();
//        Set<String> set = properties.stringPropertyNames(); //获取java虚拟机和系统的信息。
//        for(String name : set){
//            System.out.println(name + ":" + properties.getProperty(name));
//        }
        String localIP = getHostIp();
        System.out.println("ip::"+localIP);
    }

    /**
     * 获取本机ip地址，并自动区分Windows还是linux操作系统
     * @return String
     */
    public static String getHostIp(){
        String sIP = "";
        InetAddress ip = null;
        try {
            //如果是Windows操作系统
            if(isWindows()){
                ip = InetAddress.getLocalHost();
            }
            //如果是Linux操作系统
            else{
                boolean bFindIP = false;
                Enumeration<NetworkInterface> netInterfaces = NetworkInterface
                        .getNetworkInterfaces();
                while (netInterfaces.hasMoreElements()) {
                    if(bFindIP){
                        break;
                    }
                    NetworkInterface ni =  netInterfaces.nextElement();
                    //----------特定情况，可以考虑用ni.getName判断
                    //遍历所有ip
                    Enumeration<InetAddress> ips = ni.getInetAddresses();
                    while (ips.hasMoreElements()) {
                        ip = ips.nextElement();
                        if( ip.isSiteLocalAddress()
                                && !ip.isLoopbackAddress()   //127.开头的都是lookback地址
                                && ip.getHostAddress().indexOf(":")==-1){
                            bFindIP = true;
                            break;
                        }
                    }

                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        if(null != ip){
            sIP = ip.getHostAddress();
        }
        return sIP;
    }


    public static boolean isWindows() {
        boolean flag = Boolean.FALSE;
        String osName = System.getProperty("os.name").toLowerCase();
        logger.debug("osName::{}", osName);
        //System.out.println(" osName:"+osName);
        if (osName.indexOf("windows") >= 0) {
            flag = Boolean.TRUE;
        }
        return flag;
    }

    public static Boolean isLinux() {
        boolean flag = Boolean.FALSE;
        String osName = System.getProperty("os.name").toLowerCase();
        logger.debug("osName::{}", osName);

        if (osName.indexOf("linux") >= 0) {
            flag = Boolean.TRUE;
        }
        return flag;
    }


}
